import axios from 'axios';
import { message } from '../axios/message';
import router from '../router/index'
const requests = axios.create({
    baseURL: "http://192.168.0.69:8085",
    headers: {
        Authorization: `${localStorage.getItem("token")}`
    }
})

requests.interceptors.request.use(config => {
    config.headers['Authorization'] = `${localStorage.getItem("token")}`;
    return config;
}, error => {
    console.log(error);
    return Promise.reject(new Error("faile"));
})

requests.interceptors.response.use(response => {
    if (response.data.status == 401) {
        message.error("用户未登录")
        // console.log(response);
        router.push({
            path: '/login'

        });
    } else if(response.data.status == 403){
        message.error("无权执行此操作")
    } else{
        if (response.data.desc != null) {
            message({
                showClose: true,
                message: response.data.desc,
                type: 'success'
            });
        }
    }
    return response;
}, error => {
    message.error(error.response.data.message)
    if (error.response.status === 401) {
        router.push({
            path: '/login'
        });
    }
    return Promise.reject(new Error("faile"));
})

export default requests;